package com.xdf.data_transmitter.service;

import com.xdf.data_transmitter.dao.cloud.ClientPushlistDetailMapper;
import com.xdf.data_transmitter.model.ClientPushlistDetail;
import com.xdf.data_transmitter.model.FinishedVatnum;
import com.xdf.data_transmitter.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClientPushlistDetailServiceImpl implements ClientPushlistDetailService {
    @Autowired
    private ClientPushlistDetailMapper dao;
    @Autowired
    private GJrkbillService jrkbillService;
    @Autowired
    private GPrintingdyeingnameService printingdyeingnameService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ClientPushlistDetail record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(ClientPushlistDetail record) {
        return dao.insertSelective(record);
    }

    @Override
    public ClientPushlistDetail selectByPrimaryKey(Long id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ClientPushlistDetail record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ClientPushlistDetail record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByMillVatnum(String mill, String vat_num) {
        return dao.deleteByMillVatnum(mill,vat_num);
    }

    @Override
    public int updateFinished(Integer lastIId) {
        String mill = printingdyeingnameService.getMillName();
        if( mill == null || mill.length()<=0 ) throw new BusinessException("未能获取到染厂名称！");
        List<FinishedVatnum> list = jrkbillService.getFinishedVatnum(lastIId);
        if( list == null || list.size()<=0 ) throw new BusinessException("没有获取到大于" + lastIId + "的打卷数据！");

        for(FinishedVatnum item:list){
            ClientPushlistDetail record = dao.selectByMillVatnum(mill,item.getVatNum());
            if( record == null ) continue;
            record.setProgress(item.getProcessName());
            record.setProcess_index(item.getSeq());
            dao.updateByPrimaryKey(record);
        }
        return list.size();
    }

    @Override
    public ClientPushlistDetail selectByMillVatnum(String mill, String vat_num) {
        return dao.selectByMillVatnum(mill,vat_num);
    }

}
